package com.apiserver.data.idStrategy;

import com.apiserver.data.mybatis.model.EntityTable;
import com.apiserver.kernel.utils.SnowFlake;
import org.apache.commons.beanutils.BeanUtils;

public class SnowflakeProvider<M> implements IdProvider<M> {

    private SnowflakeProvider() {};//构造私有，防止开发者实例化类Single，创建出新的single

    //饿汉式：类一加载就创建对象  类成员变量私有，使成员变量不能直接修改这个值,只能通过public 方法访问

    private static SnowflakeProvider s = new SnowflakeProvider();
    public static SnowflakeProvider getSingle() {
        return s;
    }


    @Override
    public boolean toGenerateId(EntityTable entity, M m) {
        try {
            BeanUtils.setProperty(m, entity.getPKProperty(), new SnowFlake(2, 3).nextId());
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;
    }
}
